package com.yxuan.springboot.controller;

import com.yxuan.springboot.model.TestScorneirong;
import com.yxuan.springboot.service.TestScorneirongService;
import com.yxuan.springboot.util.COSImage;
import com.yxuan.springboot.util.CodeUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author zchao
 * TestScorneirong控制器
 * @date 2023-05-16 14:22:56
 */
@Controller
@Slf4j
@Api(tags = "")
@RequestMapping(value = "/testscorneirong", method = {RequestMethod.POST, RequestMethod.GET})
public class TestScorneirongController extends BaseController<TestScorneirong> {

    @Autowired
    private TestScorneirongService testScorneirongService;

//     添加 测试 的 成绩区间表信息。
    @ResponseBody
    @RequestMapping("/addscore")
    public HashMap<String,Object> addScore(@RequestBody TestScorneirong entity){
        try{
            return testScorneirongService.addScore(entity);
        }catch (Exception e){
            HashMap<String,Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result",ERROR);
            return result;
        }
    }




    //    TODO 上传文件
    @PostMapping("/uploadpdf")
    @ResponseBody
    public  HashMap<String,Object>  handlePdfUpload(MultipartFile file){
//        获取上传文件信息
        try {
            HashMap<String,Object> result = new HashMap<>();
            if (file != null) {
                //1.获取源文件的输入流
                InputStream is = file.getInputStream();
                String filename;
                String originalFilename = file.getOriginalFilename();
                String contentType = file.getContentType();
                long size = file.getSize();
                String localPath = System.getProperty("user.dir") + "/src/main/resources/img/pdf";
//        根据文件名获取文件后缀名
                String suffixName = contentType.substring(contentType.indexOf("/") + 1);
                //            只允许上传图片，若上传非图片，提醒上传图片
                if (!suffixName.equals("pdf")) {
                    result.put("error","上传不是pdf");
                    return result;
                }
//            得到文件名
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                filename = uuid + "." + suffixName;



                //4.通过上传路径得到上传的文件夹
                File fileLoad = new File("/root/upload/images");
                //4.1.若目标文件夹不存在，则创建
                if(!fileLoad.exists()){ //判断目标文件夹是否存在
                    fileLoad.mkdirs();//4.2.不存在，则创建文件夹
                }
                //5.根据目标文件夹和目标文件名新建目标文件（上传后的文件）
                File newFile = new File("/root/upload/images",filename);  //空的目标文件
                //6.根据目标文件的新建其输出流对象
                FileOutputStream os = new FileOutputStream(newFile);
                //7.完成输入流到输出流的复制
                IOUtils.copy(is,os);
                //8.关闭流(先开后关)
                os.close();
                is.close();

                try {
                    String baseUrl = "https://xc1.haoxnet.com:8098/images/";
                    String fileUrl = filename;
                    result.put("success",baseUrl+fileUrl);
                    return result;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }



//                COSImage cosImage = new COSImage();
////            文件保存路径
//                file.transferTo(new File(new File(localPath).getAbsolutePath() + "/" + filename));
//                String localFilePath = System.getProperty("user.dir") + "/src/main/resources/img/pdf/" + filename;
////            将保存在本地的头像上传到腾讯云-对象存储
//                String key = "zypdf/"+filename;
//                cosImage.onLoad_img(filename, localFilePath,key);
////            }
////        获取头像的网址
//                String http = "https://resource-1316563429.cos.ap-nanjing.myqcloud.com/zypdf/" + filename;
//                //        执行完毕，删除本地文件
//                File localfile = new File(localFilePath);
//                localfile.delete();









            }
//            上传文件为空，
            return null;
        } catch (Exception e) {
            HashMap<String,Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result",ERROR);
            return result;
        }
    }
}
